// pages/shopInfo/experience/experience.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: null,
        id: null,
        company: '',
        startDate: '入职时间',
        endDate: '离职时间',
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.type == 'edit') {
            this.setData({
                type: options.type,
                id: wx.getStorageSync('experienceInfo').store_experience_id,
                company: wx.getStorageSync('experienceInfo').name,
                startDate: wx.getStorageSync('experienceInfo').time_bigan,
                endDate: wx.getStorageSync('experienceInfo').time_end
            })
        } else {
            this.setData({
                type: options.type
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    companyInput: function(e) {
        this.setData({
            company: e.detail.value
        })
    },

    bindStartDateChange: function(e) {
        if (this.data.endDate != '离职时间' && e.detail.value > this.data.endDate) {
            wx.showToast({
                title: '在职时间不能大于离职时间',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            startDate: e.detail.value
        })
    },

    bindEndDateChange: function(e) {
        if (this.data.startDate != '入职时间' && e.detail.value < this.data.startDate) {
            wx.showToast({
                title: '离职时间不能小于在职时间',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            endDate: e.detail.value
        })
    },

    saveExperience: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.company == '') {
            wx.showToast({
                title: '请填写公司名称',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.startDate == '') {
            wx.showToast({
                title: '请选择入职时间',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.endDate == '') {
            wx.showToast({
                title: '请选择离职时间',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '信息提交中'
        })
        const that = this
        if (this.data.type == 'add') {
            let data = {
                store_id: wx.getStorageSync('storeId'),
                name: this.data.company,
                time_bigan: Date.parse(new Date(this.data.startDate)) / 1000,
                time_end: Date.parse(new Date(this.data.endDate)) / 1000
            }
            common.post('Store/add_store_experience', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 2000
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '信息提交成功',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        }
                    })
                }
            }).catch(function(e) {})
        } else {
            let data = {
                store_experience_id: this.data.id,
                name: this.data.company,
                time_bigan: Date.parse(new Date(this.data.startDate)) / 1000,
                time_end: Date.parse(new Date(this.data.endDate)) / 1000
            }
            common.post('Store/up_store_experience', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 2000
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '信息提交成功',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        }
                    })
                }
            }).catch(function(e) {})
        }
    },

    goDelete: function() {
        const that = this
        let data = {
            store_experience_id: this.data.id
        }
        common.post('Store/delete_store_experience', data).then(function(res) {
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '删除成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }
        }).catch(function(e) {})
    }
})